import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flutter/material.dart';
import 'package:flutter_flame/worlds/game/state/SeedSelectionState.dart';
import 'package:flutter_flame/config/AssetsName.dart';
import 'package:flutter_flame/component/SpriteEntity.dart';
import '../TextInitSunlight.dart';
import 'SeedPacketSilhouette.dart';

class SeedBank extends SpriteEntity {
  final SeedSelectionState state;

  SeedBank({required this.state}) : super(AssetsName.spriteSeedChooserSeedBank);

  static const double _bankWidth = 535.0;
  static const double _yOffsetHidden = -189;
  static const double _slotStartX = 108;
  static const double _slotY = -180;
  static const double _slotGap = 68;

  @override
  Future<void> onLoad() async {
    await super.onLoad();
    _setInitialSizeAndPosition();
    _initSeedSlots();
    _addInitSunlightText();
  }

  // 添加占位格
  void _initSeedSlots() {
    for (int i = 0; i < state.maxSeedsLength; i++) {
      state.seedBankPositions.add(Vector2(_slotStartX + i * _slotGap, _slotY));
    }
    for (int i = 0; i < state.maxSeedsLength; i++) {
      add(SeedPacketSilhouette(
        i: i,
        j: 0,
        startX: _slotStartX,
        startY: 10,
        gap: 8,
      ));
    }
  }

  // 添加阳光文字
  void _addInitSunlightText() {
    add(TextInitSunlight());
  }

  // 移入
  void moveIn({double duration = 0.4}) {
    add(
      MoveToEffect(
        Vector2(0, _yOffsetHidden),
        EffectController(duration: duration, curve: Curves.easeInOut),
      ),
    );
  }

  // 初始化位置信息
  void _setInitialSizeAndPosition() {
    final radio = sprite!.image.width / sprite!.image.height;
    setSize(_bankWidth);
    position = Vector2(0, _yOffsetHidden - _bankWidth / radio);
  }
}
